# create directories

run_test() {

    arch=$1
    script=$2
    xmatrix_dir=$3

    original_matrix_comp_file=$xmatrix_dir/$arch/xmatrix_comp_file.xf
    matrix_comp_file=$xmatrixdir/xmatrix_comp_file_rndperm.xf
    decompress_matrix_str="
    rm -rf $matrix_comp_file $matrix_decompressed_file ; \
    xrnlib-cli \
        --decoding-conf -compiled-param -no-password \
        --encoding-conf -compiled-param -no-password \
        --sequence-conf -xmatrix-comp $original_matrix_comp_file \
        --permutation-conf -arch $arch \
        --permute-xmatrix-comp-random -xmatrix-comp-dst $matrix_comp_file ; \
    xrnlib-cli \
        --decoding-conf -compiled-param -no-password \
        --encoding-conf -compiled-param -no-password \
        --sequence-conf -xmatrix-comp $matrix_comp_file \
        --permutation-conf -arch $arch \
        --decompress-xmatrix -xmatrix $matrix_decompressed_file
    "
    eval $decompress_matrix_str

    output_dir=$current_dir../../../cache/tests/${arch}_$script
    if [ ! -d $output_dir ]; then
        mkdir $output_dir
    fi
    rm -rf $output_dir/*
    echo "# cd ../../../scripts/backend/parser_tests ; rm -rf $output_dir/* ;" > $parser_reports_dir/${arch}_$script.log
    echo $decompress_matrix_str >> $parser_reports_dir/${arch}_$script.log
    ./$script $arch $matrix_decompressed_file $matrix_comp_file $output_dir $lorem_ipsum_file \
        1>>$parser_reports_dir/${arch}_$script.log \
        2>$parser_reports_dir/${arch}_$script.err

    str="${arch}_$script FAIL"
    if  cat $parser_reports_dir/${arch}_$script.log | awk '/^FAIL/ {print $0}' | grep -q FAIL ; then
        str="${arch}_$script FAIL"
    elif [ -s $parser_reports_dir/${arch}_$script.err ] ; then
        str="${arch}_$script FAIL"
    elif cat $parser_reports_dir/${arch}_$script.log | awk '/^lexer error/ {print $0}' | grep -q error ; then
        str="${arch}_$script FAIL"
    else
        str="${arch}_$script PASS"
    fi
    echo $str >> $parser_reports_dir/regression.log ; echo $str

}


current_dir=""
#current_dir=$(pwd)/

build_dir=$current_dir../../../build

if [ ! -d $build_dir ] ; then
    mkdir $build_dir
fi

build_reports_dir=$build_dir/reports
if [ ! -d $build_reports_dir ] ; then
    mkdir $build_reports_dir
fi

parser_reports_dir=$build_reports_dir/parser_tests
if [ ! -d $parser_reports_dir ] ; then
    mkdir $parser_reports_dir
fi
rm -rf $parser_reports_dir/*

# decompress the xoron matrix compressed with the right architecture

xmatrixdir=$current_dir../../../cache/tests/xmatrixdir
matrix_decompressed_file=$xmatrixdir/xmatrix_file.xf


[ ! -d ../../../cache ] && mkdir ../../../cache
[ ! -d ../../../cache/tests ] && mkdir ../../../cache/tests
[ ! -d ../../../cache/tests/xmatrixdir ] && mkdir ../../../cache/tests/xmatrixdir

# generate xoron matrices compressed if they are not present

./generate_compressed_matrices.sh $xmatrixdir

# select permutation layer and architecture randomly

rnd=$(( $RANDOM % 2 ))
if   [ $rnd -eq 0 ] ; then arch=128
elif [ $rnd -eq 1 ] ; then arch=256
fi

# copy lorem ipsum
cp $current_dir../../../archives/resources/code_examples/lorem_ipsum.txt $xmatrixdir/
lorem_ipsum_file=$xmatrixdir/lorem_ipsum.txt

rm -rf ../../../cache/tests/128_*
rm -rf ../../../cache/tests/256_*

# get the list of scripts and select one test

script_lst=$(ls . | sed '$aall' | grep -v generate_compressed_matrices.sh | grep -v run_parser_tests.sh | grep -v check_coverage_functions.sh )
if [ -z "$1" ] ; then
    echo "test list: "
    echo ${script_lst[@]} | tr -s ' ' '\n' | cat -n
    printf "select a test: "
    read -r test
else
    test=$1
fi

# run a test

i=0
all_index=0
for script in ${script_lst[@]} ; do

   i=$(( i + 1 ))

   if [ "$test" == "all" ] ; then

      all_index=$(( i - 1 ))

   else

      if [ $i -eq $test ] ; then
         if [ $script == "all" ] ; then
             all_index=$(( i - 1 ))

         else
            run_test $arch $script $xmatrixdir

         fi
      fi
   fi
done

# run all the tests

i=0
if [ $all_index -ge 1 ]; then
    for script in ${script_lst[@]} ; do

        i=$(( i + 1 ))

        if [ $i -le $all_index ] ; then

            arch=128;
            run_test $arch $script $xmatrixdir

            arch=256;
            run_test $arch $script $xmatrixdir

        fi
    done

    if cat $parser_reports_dir/regression.log | grep -q FAIL ; then
        echo "regression FAIL" >> $parser_reports_dir/regression.log
        echo "regression FAIL"
    else
        echo "regression PASS" >> $parser_reports_dir/regression.log
        echo "regression PASS"
    fi

fi
